﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace HML
{
    /// <summary>
    ///圆角
    /// </summary>
    [Serializable]
    [TypeConverterAttribute(typeof(RoundCornerConverter))]
    public class RoundCorner
    {
        #region 字段

        private bool _all;
        private int _leftTop;
        private int _rightTop;
        private int _rightBottom;
        private int _leftBottom;

        public static readonly RoundCorner Empty = new RoundCorner(0);

        #endregion

        #region 属性

        /// <summary>
        /// 全部
        /// </summary>
        [Description("全部")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int All
        {
            get
            {
                return this._all ? this._leftTop : -1;
            }
            set
            {
                if (value < 0)
                {
                    return;
                }

                if (this._all != true || this._leftTop != value)
                {
                    this._all = true;
                    this._leftTop = this._rightTop = this._rightBottom = this._leftBottom = value;
                }
            }
        }

        /// <summary>
        /// 左上角
        /// </summary>
        [Description("左上角")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int LeftTop
        {
            get
            {
                if (this._all)
                {
                    return this._leftTop;
                }
                return this._leftTop;
            }
            set
            {
                if (value < 0)
                {
                    return;
                }

                if (this._all || this._leftTop != value)
                {
                    this._all = false;
                    this._leftTop = value;
                }
            }
        }

        /// <summary>
        /// 右上角
        /// </summary>
        [Description("右上角")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int RightTop
        {
            get
            {
                if (this._all)
                {
                    return this._leftTop;
                }
                return this._rightTop;
            }
            set
            {
                if (value < 0)
                {
                    return;
                }

                if (this._all || this._rightTop != value)
                {
                    this._all = false;
                    this._rightTop = value;
                }
            }
        }

        /// <summary>
        /// 右下角
        /// </summary>
        [Description("右下角")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int RightBottom
        {
            get
            {
                if (this._all)
                {
                    return this._leftTop;
                }
                return this._rightBottom;
            }
            set
            {
                if (value < 0)
                {
                    return;
                }

                if (this._all || this._rightBottom != value)
                {
                    this._all = false;
                    this._rightBottom = value;
                }
            }
        }

        /// <summary>
        /// 左下角
        /// </summary>
        [Description("左下角")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int LeftBottom
        {
            get
            {
                if (this._all)
                {
                    return this._leftTop;
                }
                return this._leftBottom;
            }
            set
            {
                if (value < 0)
                {
                    return;
                }

                if (this._all || this._leftBottom != value)
                {
                    this._all = false;
                    this._leftBottom = value;
                }
            }
        }

        #endregion

        #region  构造

        /// <summary>
        /// 
        /// </summary>
        /// <param name="all">全部圆角</param>
        public RoundCorner(int all)
        {
            this._all = true;
            this._leftTop = this._rightTop = this._rightBottom = this._leftBottom = all;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="leftTop">左上角</param>
        /// <param name="rightTop">右上角</param>
        /// <param name="rightBottom">右下角</param>
        /// <param name="leftBottom">左下角</param>
        public RoundCorner(int leftTop, int rightTop, int rightBottom, int leftBottom)
        {
            this._leftTop = leftTop;
            this._rightTop = rightTop;
            this._rightBottom = rightBottom;
            this._leftBottom = leftBottom;
            this._all = (this._leftTop == this._rightTop && this._leftTop == this._rightBottom && this._leftTop == this._leftBottom);
        }

        #endregion

        #region 运算符重载

        /// <summary>
        /// 判断两个圆角对象设置是否一样
        /// </summary>
        /// <param name="rc1"></param>
        /// <param name="rc2"></param>
        /// <returns></returns>
        public static bool operator ==(RoundCorner rc1, RoundCorner rc2)
        {
            return rc1.LeftTop == rc2.LeftTop && rc1.RightTop == rc2.RightTop && rc1.RightBottom == rc2.RightBottom && rc1.LeftBottom == rc2.LeftBottom;
        }

        /// <summary>
        /// 判断两个圆角对象设置是否不一样
        /// </summary>
        /// <param name="rc1"></param>
        /// <param name="rc2"></param>
        /// <returns></returns>
        public static bool operator !=(RoundCorner rc1, RoundCorner rc2)
        {
            return !(rc1 == rc2);
        }

        #endregion

        #region 重写

        /// <summary>
        /// 获取该对象的哈希值（参考源码）
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.LeftTop ^ RotateLeftTop(this.RightTop, 8) ^ RotateLeftTop(this.RightBottom, 16) ^ RotateLeftTop(this.LeftBottom, 24);
        }

        /// <summary>
        /// 判断是否和该对象的引用是一样
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public override bool Equals(object other)
        {
            if (other is RoundCorner)
            {
                return ((RoundCorner)other) == this;
            }
            return false;
        }

        /// <summary>
        /// 把该对象转换成字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "{LeftTop=" + this.LeftTop.ToString(CultureInfo.CurrentCulture) + ",RightTop=" + this.RightTop.ToString(CultureInfo.CurrentCulture) + ",RightBottom=" + this.RightBottom.ToString(CultureInfo.CurrentCulture) + ",LeftBottom=" + this.LeftBottom.ToString(CultureInfo.CurrentCulture) + "}";
        }

        #endregion

        #region 方法

        /// <summary>
        /// 计算哈希值（参考源码）
        /// </summary>
        /// <param name="value"></param>
        /// <param name="nBits"></param>
        /// <returns></returns>
        public static int RotateLeftTop(int value, int nBits)
        {
            nBits = nBits % 32;
            return value << nBits | (value >> (32 - nBits));
        }

        /// <summary>
        /// 是否应该使用All设置初始化对象
        /// </summary>
        /// <returns></returns>
        public bool ShouldSerializeAll()
        {
            return this._all;
        }

        #endregion
    }

}
